﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
namespace Data
{
	 	//TradeLog
		public class TradeLog
	{	
		/// <summary>
        /// 主键
        /// </summary>
        private readonly string primaryKey= "Id";
		/// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(Model.TradeLog model)
        {
            return new DataAccess().Add<Model.TradeLog>(model, primaryKey);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.TradeLog model)
        {
            return new DataAccess().Update<Model.TradeLog>(model, primaryKey);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(long Id)
        {
            return new DataAccess().Delete<Model.TradeLog>(Id, primaryKey);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.TradeLog GetModel(long Id)
        {
            return new DataAccess().Get<Model.TradeLog>(Id, primaryKey);
        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public int GetAll_Count()
        {
            return new DataAccess().GetCount<Model.TradeLog>(primaryKey);
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public List<Model.TradeLog> GetPageList_Where(int PageIndex, int PageSize, string OrderID,out int totalCount)
        {          
            string strWhere = GetSqlWhere(OrderID);
            totalCount = new DataAccess().GetCount<Model.TradeLog>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.TradeLog>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(string OrderID)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" 1>0");

            if (!string.IsNullOrEmpty(OrderID) && !string.IsNullOrEmpty(OrderID.Trim()))
            {
                sqlWhere.Append(" and OrderID like '%" + OrderID + "%'");
            }         
            return sqlWhere.ToString();
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public int Drop(string Ids)
        {
            return new DataAccess().Drop<Model.TradeLog>(Ids, primaryKey);
        }

        public Model.TradeLog GetEffectivePay(long orderID)
        {
            string sql = "select top 1 * from [TradeLog] where OrderID=" + orderID + " and Type=" + Model.RRBEnum.OrderTradeType.支付.GetHashCode() + " and Flag=1 order by Id desc";
            IList<Model.TradeLog> list = new DataAccess().GetAll<Model.TradeLog>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
        public Model.TradeLog GetLastByOrderID(long orderID)
        {
            string sql = "select top 1 * from [TradeLog] where OrderID=" + orderID + " order by Id desc";

            IList<Model.TradeLog> list = new DataAccess().GetAll<Model.TradeLog>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
        public Model.TradeLog GetBySerialNumber(string serialNumber)
        {
            string sql = "select top 1 * from [TradeLog] where SerialNumber='" + serialNumber + "' order by Id desc";

            IList<Model.TradeLog> list = new DataAccess().GetAll<Model.TradeLog>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
	}
}